<?php
namespace Admin\Controller;
use Think\Controller;
class PromotionController extends CommonController{


	/**
	 * 推广人列表
	 */
	public function promoter_lists(){

		if(IS_GET){

			$wx_qrcode = M('wx_qrcode');

			$lists = $wx_qrcode->order("id DESC")->select();

			foreach ($lists as $key => $value) {
				# code...
				$lists[$key]['follower_count'] = M('wx_qrcode_member_relationship')->where(array('msg' => $value['msg']))->count();
			}
			// print_r($lists); die;
			$this->assign('lists',$lists);
			$this->display();
		}
	}


	/**
	 * 获取推广人粉丝
	 */
	public function promoter_follower(){

		if(IS_GET){

			$msg = I('get.msg');
			// echo $id; die;
			$followers = M('wx_qrcode_member_relationship');

			$sql = "select fd_wx_qrcode_member_relationship.*,fd_members.username as username from fd_wx_qrcode_member_relationship left join fd_members on fd_wx_qrcode_member_relationship.openid = fd_members.wxopenid where fd_wx_qrcode_member_relationship.msg=".$msg;

			$lists = $followers->query($sql);
			// print_r($lists);
			$this->assign('lists',$lists);
			$this->display();
		}
	}



	/**
     * 删除粉丝
     */
    public function del_follower(){

        if(IS_AJAX){

            $id = I('post.id');
            if(!empty($id)){

                $map['id'] = $id;
            }

            $wx_qrcode_member_relationship = M('wx_qrcode_member_relationship');

            $re = $wx_qrcode_member_relationship->where($map)->delete();

            if($re){

                echo json_encode(1);
            }else{

                echo json_encode(0);
            }
        }
    }


    /**
     * 删除推广人
     */
    public function del_promoter(){

        if(IS_AJAX){

            $id = I('post.id');
            if(!empty($id)){

                $map['id'] = $id;
            }

            $wx_qrcode = M('wx_qrcode');

            $re = $wx_qrcode->where($map)->delete();

            if($re){

                echo json_encode(1);
            }else{

                echo json_encode(0);
            }
        }
    }




    /**
     * 添加新的推广人
     */
    public function add_promoter(){

        if(IS_GET){

            $scene_str = $this->get_scene_str();

            $this->assign('scene_str',$scene_str);
            $this->display();
        }else{

            $promoter = I('post.promoter');
            if(!empty($promoter)){

                $data['promoter'] = $promoter;
            }

            $url = I('post.url');
            if(!empty($url)){

                $data['url'] = $url;
            }

            $msg = I('post.msg');
            if(!empty($msg)){

                $data['msg'] = $msg;
            }   

            $data['ctime'] = I('server.REQUEST_TIME');
            
            $wx_qrcode = M('wx_qrcode');

            $res = $wx_qrcode->data($data)->add();

            if(!empty($res)){

                $this->success('添加推广人成功!',U('Admin/Promotion/promoter_lists'),1);
            }else{

                $this->error('添加推广人失败!','',1);
            }
        }
    }



    /**
     * 生成二维码
     */
    public function get_franchise_qrcode() {

        $scene_str = I('post.scene_str');

        $accessToken = $this->getAccessToken();
        // echo $accessToken; die;
        $wxsdk = new \Com\WechatAuth(C('appid'), C('appsecret'),$accessToken);

        $qrcode_res = $wxsdk->qrcodeCreate(0,$scene_str);
        // print_r($qrcode_res); die;
        if(!isset($qrcode_res['errcode'])){

            //不存在 errcode 则说明得到正常返回数据
            $qrcode_img = $this->get_qrcode_by_ticket($qrcode_res['ticket']);
            
            $res['qrcode_img'] = $qrcode_img;
            $res['scene_str'] = $scene_str;
        }
        echo json_encode($res);
    }



    /*
     * 保存access_token 的最新状态
     * 
     */
    public function getAccessToken() {

        $jssdk = new \Com\JSSDK(C('appid'), C('appsecret'));
        $access_token = $jssdk->returnAccessToken();
        return $access_token;
    }


    /**
     * 通过ticket获取二维码图片
     * 
     */
    public function get_qrcode_by_ticket($ticket){


        $ticket = urlencode($ticket);

        $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket;

        return $url;
    }


    //生成随机微信scene_str
    private function get_scene_str(){

        $time=I('server.REQUEST_TIME');
        return 'zctx'.$time.str_pad(mt_rand(1,999999),5,'0', STR_PAD_LEFT);
    }

}